﻿/*
输入：
6 7
5 6 -2
1 2 1
5 3 1
2 5 2
2 4 -3
4 6 4
1 3 5
输出：
1
*/
// n个城市，m条路
string[] nm = Console.ReadLine().Split(" ");
int n = int.Parse(nm[0]);
int m = int.Parse(nm[1]);

// 构建图 -- 邻接表
List<(int, int)>[] graph = new List<(int, int)>[n+1];
for (int i = 0; i <= n; i++)
{
    graph[i] = new List<(int, int)>();
}

for (int i = 0; i < m; i++)
{
    string[] stv = Console.ReadLine().Split(" ");
    int s = int.Parse(stv[0]);
    int t = int.Parse(stv[1]);
    int v = int.Parse(stv[2]);
    graph[s].Add((t, v));
}

// Bellman_ford 算法
int[] minDist = new int[n + 1];
Array.Fill(minDist, int.MaxValue);
minDist[1] = 0;
Queue<int> queue = new Queue<int>();
queue.Enqueue(1);
bool[] inQueue = new bool[n + 1]; // 在队列中

while(queue.Count > 0)
{
    int index = queue.Dequeue();
    inQueue[index] = false;
    for (int i = 0; i < graph[index].Count; i++)
    {
        (int t, int v) = graph[index][i];
        if (!inQueue[t])
        {
            queue.Enqueue(t);
            inQueue[t] = true;
        }
        
        minDist[t] = minDist[t] > minDist[index] + v ? minDist[index] + v : minDist[t];
    }
}
Console.WriteLine(minDist[n] == int.MaxValue ? "unconnected":minDist[n]);